//
//  YJPopupView.m
//  YJPopupView
//
//  Created by Jim1024 on 2016/10/10.
//  Copyright © 2016年 Sunnsoft Co., Ltd. All rights reserved.
//

#import "YJPopupView.h"

static CGFloat const kAnimationDuration = 0.25;

@interface YJPopupView ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIWindow *popWindow;
@property (nonatomic, strong) UIView *maskView; // 遮罩层

@property (nonatomic, strong) UITapGestureRecognizer *dismissTap;
/// 弹窗样式 默认是 YJPopupViewTypeCenter
@property (nonatomic, assign) YJPopupViewType popupType;

@end

@implementation YJPopupView

static YJPopupView *shareView;

#pragma mark - public method

+ (void)addContentView:(UIView *)contentView {
    shareView = [[self alloc] initWithContentView:contentView];
}

+ (void)dismissWhenTapMaskView:(BOOL)enable {
    if (!enable) {
        [shareView.maskView removeGestureRecognizer:shareView.dismissTap];
    }
}

+ (void)setPopopType:(YJPopupViewType)type {
    [shareView setPopupType:type];
}

+ (void)show {
    [shareView show];
}

+ (void)showInView:(UIView *)view {
    [shareView showInView:view];
}

+ (void)dismiss {
    [shareView dismiss];
}

- (instancetype)initWithContentView:(UIView *)contentView {
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
        self.frame = [UIScreen mainScreen].bounds;
        _contentView = contentView;
        _popupType = YJPopupViewTypeCenter;
        [self initCostomView];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"YJPopupView dealloc");
}

- (void)initCostomView {
    
    self.maskView = ({
        UIView *maskView = [[UIView alloc] initWithFrame:self.frame];
        _dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [maskView addGestureRecognizer:_dismissTap];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0.0;
        maskView;
    });
    [self addSubview:self.maskView];
}

- (void)show {
    [self addSubview:self.contentView];
    [self.popWindow makeKeyAndVisible];
    [self.popWindow addSubview:self];
    
    if (self.popupType == YJPopupViewTypeCenter) {
        self.contentView.center = self.center;
        self.contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
        [UIView animateWithDuration:kAnimationDuration delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:0 animations:^{
            self.contentView.transform = CGAffineTransformIdentity;
        } completion:nil];
        [UIView animateWithDuration:kAnimationDuration animations:^{
            self.maskView.alpha = 0.3;
        }];
    } else if (self.popupType == YJPopupViewTypeBottom) {
        CGRect tempFrame = self.contentView.frame;
        tempFrame.origin.y = self.bounds.size.height;
        self.contentView.frame = tempFrame;
        [UIView animateWithDuration:kAnimationDuration animations:^{
            self.maskView.alpha = 0.3;
            CGRect tempFrame = self.contentView.frame;
            tempFrame.origin.y = self.bounds.size.height - tempFrame.size.height;
            self.contentView.frame = tempFrame;
        }];
    }
}

- (void)showInView:(UIView *)view {
    [self addSubview:self.contentView];
    [view addSubview:self];
    
    if (self.popupType == YJPopupViewTypeCenter) {
        self.contentView.center = self.center;
        self.contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
        [UIView animateWithDuration:kAnimationDuration delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:0 animations:^{
            self.contentView.transform = CGAffineTransformIdentity;
        } completion:nil];
        [UIView animateWithDuration:kAnimationDuration animations:^{
            self.maskView.alpha = 0.3;
        }];
    } else if (self.popupType == YJPopupViewTypeBottom) {
        CGRect tempFrame = self.contentView.frame;
        tempFrame.origin.y = self.bounds.size.height;
        self.contentView.frame = tempFrame;
        [UIView animateWithDuration:kAnimationDuration animations:^{
            self.maskView.alpha = 0.3;
            CGRect tempFrame = self.contentView.frame;
            tempFrame.origin.y = self.bounds.size.height - tempFrame.size.height;
            self.contentView.frame = tempFrame;
        }];
    }
}

- (void)dismiss {
    [UIView animateWithDuration:kAnimationDuration animations:^{
        self.maskView.alpha = 0;
        if (self.popupType == YJPopupViewTypeCenter) {
            self.contentView.transform = CGAffineTransformMakeScale(0.0, 0.0);
        } else if (self.popupType == YJPopupViewTypeBottom) {
            CGRect tempFrame = self.contentView.frame;
            tempFrame.origin.y = self.bounds.size.height;
            self.contentView.frame = tempFrame;;
        }
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (_popWindow) {
            [self.popWindow resignKeyWindow];
            self.popWindow = nil;
            shareView = nil;
        }
    }];
}

#pragma mark - Setter & Getter

- (UIWindow *)popWindow {
    if (_popWindow == nil) {
        _popWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _popWindow.windowLevel = UIWindowLevelAlert;
        _popWindow.backgroundColor = [UIColor clearColor];
    }
    return _popWindow;
}

@end
